
// 造一些树数据
function treeToList(treeData) {
    const result = [];
   
    function traverse(node) {
      const item = {
        id: node.id,
        name: node.name,
        children: [],
      };
      if (node.children) {
        node.children.forEach((child) => {
          traverse(child);
        });
        item.children = treeToList(node.children);
      }
      result.push(item);
    }
   
    treeData.forEach((node) => {
      traverse(node);
    });
   
    return result;
   }
function createTreeData(level = 5) {
    let num = 0
    let treeData = [{ id: 'root', name: 'root', code: 'root' }]
    let recusion = (treeData,num) => {
        // console.log('num',num)
        if (num >= level) {
            return
        }        
        let newNum = num+1
        treeData.forEach(item => {
            let pid = item.id
            item.children = Array.from({ length: 10 }, (v, i) => {
                return {
                    id: `${pid}-${i}`,
                    name: `${newNum}-${i}`,
                    code: `${newNum}-${i}`,
                    pid,
                }
            })
            recusion(item.children,newNum)
        })

    }
    recusion(treeData,num)
    return treeData
}
var treeData = createTreeData()
var listData = treeToList(treeData);
console.log(listData);